package pl.kosmowska.ewelina.client.views.impl;

import pl.kosmowska.ewelina.client.app.requestfactory.dto.UserDataProxy;
import pl.kosmowska.ewelina.client.views.SignInView;
import pl.kosmowska.ewelina.client.views.widgets.LoadingWidget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.URL;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

public class SignInViewImpl extends Composite implements SignInView {

	private Presenter presenter;

	private boolean alreadyInit;

	private static SignInViewImplUiBinder uiBinder = GWT.create(SignInViewImplUiBinder.class);
	@UiField LoadingWidget wLoading;
	@UiField HTML htmlSignIn;

	interface SignInViewImplUiBinder extends UiBinder<Widget, SignInViewImpl> {
	}

	public SignInViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}


	@Override
	public void showLoading(boolean ifShow){
		wLoading.showLoading(ifShow);
	}

	@Override
	public void setLoggedOut(UserDataProxy userData) {
		if (userData == null) {
			// this shouldn't happen, b/c we need the urls
			return;
		}

		String url = userData.getLoginUrl();
		String qs = Window.Location.getQueryString();
		if (qs != null) {
			url += URL.encode(qs);
		}

		// This is a must, always clean before draw
		SafeHtmlBuilder builder = new SafeHtmlBuilder();
		builder.appendHtmlConstant("<a href='" + url + "'>")
		.appendEscaped("Zaloguj się")
		.appendHtmlConstant("</a>");
		htmlSignIn.setHTML(builder.toSafeHtml());
	}
	
	@Override
	public boolean isAlreadyInit() {
		return alreadyInit;
	}
	@Override
	public void setAlreadyInit(boolean alreadyInit) {
		this.alreadyInit = alreadyInit;
	}



}
